package com.heima.wemedia.listener;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.api.feign.ApArticleFeignClient;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.impl.WmNewsServiceImpl;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @Description RabbitMqListener
 * @Author GGBond
 * @Date 2024-11-27
 */
@Component
public class RabbitMqListener {

    @Autowired
    private ApArticleFeignClient apArticleFeignClient;

    @Autowired
    private WmNewsServiceImpl wmNewsService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "delay.queue"),
    exchange = @Exchange(name = "delay.exchange",delayed = "true"),key = "delay.key"))
    public void listenDelayMsg(byte[] message){
        String jsonString = new String(message, StandardCharsets.UTF_8);
        ArticleDto dto = BeanUtil.toBean(jsonString, ArticleDto.class);
        apArticleFeignClient.save(dto);
        wmNewsService.update(Wrappers.<WmNews> lambdaUpdate().eq(WmNews::getArticleId,dto.getId())
                .set(WmNews::getReason,"已发布").set(WmNews::getStatus,(short)9));
    }
}
